package com.itheima;

import com.itheima.pojo.User;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.text.SimpleDateFormat;

/**
 * @author 张一恒
 * @version 1.0
 * @description:
 * @date 2022/12/11 15:00
 */
@SpringBootTest
public class POIDemo03 {


    @Test
    public void testPoi() throws Exception {
        //读取excel文件数据
        //读取工作表
        Workbook workbook = new XSSFWorkbook("D:\\practice\\excel-practice\\用户导入测试数据.xlsx");

        //设置读取工作表索引
        Sheet sheetAt = workbook.getSheetAt(0);

        //设置读取内容 获取到最后一行内容的索引
        int lastRowIndex = sheetAt.getLastRowNum();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        //        以下三个为了节省栈内存，所以提到循环的外面
        User user = null;
        Row row = null;  //一行
        Cell cell = null; //单元格
        for (int i = 1; i <= lastRowIndex; i++) {
            row = sheetAt.getRow(i);
            user = new User();

            user.setUserName(row.getCell(0).getStringCellValue());  //用户名

            try {
                user.setPhone(row.getCell(1).getStringCellValue()); //手机号
            } catch (Exception e) {
                user.setPhone(row.getCell(1).getNumericCellValue()+""); //手机号
            }

            user.setProvince(row.getCell(2).getStringCellValue()); //省份

            user.setCity(row.getCell(3).getStringCellValue()); //城市

            user.setSalary(((Double) row.getCell(4).getNumericCellValue()).intValue());  //工资

            user.setHireDate(sdf.parse(row.getCell(5).getStringCellValue())); //入职时间

            user.setBirthday(sdf.parse(row.getCell(6).getStringCellValue())); //生日

            user.setAddress(row.getCell(7).getStringCellValue());   //现住址

            System.out.println(user.getUserName());
            System.out.println(user.getPhone());
            System.out.println(user.getProvince());
            System.out.println(user.getCity());
            System.out.println(user.getSalary());
            System.out.println(user.getHireDate());
            System.out.println(user.getBirthday());
            System.out.println(user.getAddress());

        }

    }
}
